package com.ben.multiple.shiro;

import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public Map<String, Object> handleException(Exception e) {
        log.error(e.getMessage(), e);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg", e.getMessage());
        return map;
    }

    @ExceptionHandler(AuthorizationException.class)
    public Map<String, Object> handleAuthorizationException(AuthorizationException e) {
        log.error(e.getMessage(), e);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 401);
        map.put("msg", "没有权限，请联系管理员授权");
        return map;
    }
}
